package indi.woniu.controller;

import com.github.pagehelper.PageInfo;
import indi.woniu.entity.Paper;
import indi.woniu.service.PaperService;
import indi.woniu.utils.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: GaN601
 * @Date: 2021/12/15 11:18
 * @Description:
 */
@Controller
@RequestMapping("/paper")
public class PaperController {
    
    private PaperService paperService;
    
    @Autowired
    public void setPaperService(final PaperService paperService) {
        this.paperService = paperService;
    }
    
    /**
     * 获取数据
     *
     * @return 结果集
     */
    @GetMapping("/")
    @ResponseBody
    public List<Paper> getPaper() {
        
        return paperService.selectAll();
    }
    
    /**
     * 查询一个字段集, 不重复
     *
     * @return 字段结果集
     */
    @GetMapping("/field/{field}")
    @ResponseBody
    public List<String> getPaper(@PathVariable("field") String field) {
        return paperService.selectByGroup(null,field);
    }
    
    /**
     * 查询paper表中的多个字段集, 不重复
     *
     * @return 字段结果集
     */
    @GetMapping("/fields")
    @ResponseBody
    public Map<String, List<String>> getPaper(@RequestParam Map<String, String> fields) {
        return paperService.selectByGroup(fields.values());
    }
    
    
    /**
     * 获取试卷, 使用过滤器
     *
     * @param field 接收的字段
     * @param value 值
     * @return 结果集
     */
    @GetMapping("/{field}/{value}")
    @ResponseBody
    public List<Map<String, Object>> getPaperUseFilter(@PathVariable("field") String field,
            @PathVariable("value") Object value) {
        return paperService.selectByFields(Util.toMap(field, value));
    }
    
    /**
     * 获取数据
     *
     * @return 影响行
     */
    @GetMapping("/paging/{pageSum}/{pageSize}")
    @ResponseBody
    public PageInfo<?> getPageData(@PathVariable("pageSum") Integer pageSum,
            @PathVariable("pageSize") Integer pageSize, @RequestParam Map<String, Object> fields) {
        return paperService.getPageData(pageSum, pageSize, fields);
    }
    
    /**
     * 更新数据
     *
     * @return 影响行
     */
    @PutMapping("/{id}")
    @ResponseBody
    public Integer update(@PathVariable("id") Integer id, @RequestParam Map<String, Object> paper) {
        paper.put("id", id);
        return paperService.update(id, paper);
    }
    
    /**
     * 删除对应 ID 的数据
     *
     * @return 影响的行数
     */
    @DeleteMapping("/{id}")
    @ResponseBody
    public Integer delete(@PathVariable("id") Integer id) {
        return paperService.delete(id);
    }
}
